package com.ansion.baseui.base.actionbar;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;

import com.ansion.baseui.R;
import com.ansion.baseui.base.click.AvoidDoubleClickListener;
import com.ansion.baseui.base.toolbar.TActionBarMenuUtil;
import com.ansion.baseui.base.toolbar.TMenuItem;
/**
 * created by zhaoyuntao
 * on 05/01/2023
 * description:
 */
class TMenuAdapter extends ListAdapter<TMenuItem, TMenuViewHolder> {
    private final OnItemClickListener onItemClickListener;

    protected TMenuAdapter(OnItemClickListener onItemClickListener) {
        super(new DiffUtil.ItemCallback<TMenuItem>() {
            @Override
            public boolean areItemsTheSame(@NonNull TMenuItem t0, @NonNull TMenuItem t1) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull TMenuItem t0, @NonNull TMenuItem t1) {
                return false;
            }
        });
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public TMenuViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new TMenuViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_actionbar_pop_menu_list_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull TMenuViewHolder tMenuViewHolder, int i) {
//        TMenuItem menuItem = getCurrentList().get(tMenuViewHolder.getBindingAdapterPosition());
        TMenuItem menuItem = getCurrentList().get(tMenuViewHolder.getAdapterPosition());
        TextView textView = tMenuViewHolder.itemView.findViewById(R.id.text_view_actionbar_pop_menu_item);
        TActionBarMenuUtil.setMenuParams(textView, menuItem);
        ImageView imageView = tMenuViewHolder.itemView.findViewById(R.id.image_view_actionbar_pop_menu_item);
        if (menuItem.hasDrawableRes()) {
            imageView.setVisibility(View.VISIBLE);
            TActionBarMenuUtil.setMenuParams(imageView, menuItem);
        } else {
            imageView.setVisibility(View.GONE);
        }
        tMenuViewHolder.itemView.setOnClickListener(new AvoidDoubleClickListener() {
            @Override
            public void onClickView(View view) {
                if (!menuItem.isEnable()) {
                    return;
                }
                View.OnClickListener listener = menuItem.getListener();
                if (listener != null) {
                    listener.onClick(view);
                }
                onItemClickListener.onItemClicked(view, menuItem);
            }
        });
    }

    public interface OnItemClickListener {
        void onItemClicked(View view, TMenuItem tMenuItem);
    }
}
